﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace NetCoreClass
{
    public class FileUtil
    {
        public static Hashtable FileCodeMap()
        {
            Hashtable ht = new Hashtable();
            ht.Add("jpg", "255216");
            ht.Add("gif", "7173");
            ht.Add("txt", "4946");
            ht.Add("png", "13780");
            ht.Add("bmp", "6677");
            ht.Add("xls", "208207");
            ht.Add("doc", "208207");
            ht.Add("ppt", "208207");
            /*文件扩展名说明

         * 4946/104116 txt
         * 7173        gif 
         * 255216      jpg
         * 13780       png
         * 6677        bmp
         * 239187      txt,aspx,asp,sql
         * 208207      xls.doc.ppt
         * 6063        xml
         * 6033        htm,html
         * 4742        js
         * 8075        xlsx,zip,pptx,mmap,zip
         * 8297        rar   
         * 01          accdb,mdb
         * 7790        exe,dll
         * 5666        psd 
         * 255254      rdp 
         * 10056       bt种子 
         * 64101       bat 
         * 4059        sgf    
         */
            return ht;
        }
        public static void DelectDir(string srcPath)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(srcPath);
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录
                foreach (FileSystemInfo i in fileinfo)
                {
                    if (i is DirectoryInfo)            //判断是否文件夹
                    {
                        DelectDir(i.FullName);
                        ////    DirectoryInfo subdir = new DirectoryInfo(i.FullName);
                        ////    subdir.Delete(true);          //删除子目录和文件
                    }
                    else
                    {
                        //如果 使用了 streamreader 在删除前 必须先关闭流 ，否则无法删除 sr.close();
                        File.Delete(i.FullName);      //删除指定文件
                    }
                }
            }
            catch (Exception e)
            {
                throw;
            }
        }
        /// <summary>
        /// 返回文件的类型代码
        /// </summary>
        /// <param name="filePath">文件名称</param>
        /// <returns></returns>
        public static string getFileExtension(string filePath)
        {
            FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            BinaryReader reader = new BinaryReader(stream);
            string fileclass = "";
            // byte buffer;
            try
            {

                //buffer = reader.ReadByte();
                //fileclass = buffer.ToString();
                //buffer = reader.ReadByte();
                //fileclass += buffer.ToString();

                for (int i = 0; i < 2; i++)
                {
                    fileclass += reader.ReadByte().ToString();
                }


            }
            catch (Exception)
            {

                throw;
            }
            reader.Close();
            stream.Close();
            return fileclass;


        }
        /// <summary>
        /// 是否图片文件
        /// </summary>
        /// <param name="filePath">文件名称</param>
        /// <returns></returns>
        public static bool IsImageFile(string filePath)
        {
            string fileclass = getFileExtension(filePath);
            switch (fileclass)
            {
                case "255216":    //jpg
                case "7173":        //gif
                case "13780":       //png
                case "6677":        //bmp
                    return true;
                default:
                    return false;

            }

        }
        public static bool IsDocFile(string filePath)
        {
            string fileclass = getFileExtension(filePath);
            switch (fileclass)
            {
                case "4946":    //txt
                case "104116":        //txt
                case "239187":       //txt,aspx,asp,sql
                case "208207":        //xls.doc.ppt
                    return true;
                default:
                    return false;

            }
        }
        /// <summary>
        /// 判断文件格式
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool IsAllowedExtension(string filePath, string fileType)
        {

            string fileclass = getFileExtension(filePath);
            if (fileclass.Equals(fileType))
            {
                return true;
            }
            else
            {
                return false;
            }



        }
    }
}
